package leetcode.offer_two_special.linked;

import java.util.HashSet;

/**
 * @author mazouri
 * @create 2021-09-06 22:45
 */
public class _22_detectCycle {
    public ListNode detectCycle(ListNode head) {
        if (head == null) return null;

        HashSet<ListNode> set = new HashSet<>();
        ListNode cur = head;
        while (cur!=null){
            if (set.contains(cur)) return cur;
            else set.add(cur);

            cur = cur.next;
        }
        return null;
    }
}
